package com.stoneframe.util.baseArray;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ArrayHelper {
	
	private static String printArray(int array[]){
		String result="";
		for(int i=0;i<array.length;i++){
			result+=array[i]+",";
		}
		return result.substring(0, result.length()-1);
	}
	
	//数组添加元素
	public static int[] insertElement(int original[],int element,int index){
		int length=original.length;
		int destination[] =new int[length+1];
		System.arraycopy(original, 0, destination, 0, index);
		destination[index]=element;
		System.arraycopy(original, index, destination, index+1, length-index);
		return destination;
	}
	
	public static String listToString(List list, char separator) {    
		StringBuilder sb = new StringBuilder();    
		for (int i = 0; i < list.size(); i++) {        
			sb.append(list.get(i)).append(separator);    
		}    
		return sb.toString().substring(0,sb.toString().length()-1);}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int array[]={2, 5, -2, 6, -3, 8, 0, -7, -1, 4};
		Arrays.sort(array);
		System.out.println("数组排序:"+printArray(array));
		System.out.println("数组查找："+Arrays.binarySearch(array,2));
		array=insertElement(array, -4, 1);
		System.out.println("数组添加元素  -4:"+printArray(array));
		ArrayList arrayList=new ArrayList();
		arrayList.add("A");
		arrayList.add("B");
		arrayList.add("C");
		Collections.reverse(arrayList);
		System.out.println("数组反转："+arrayList);
		Integer[] numbers = { 8, 2, 7, 1, 4, 9, 5};
		System.out.println("最小值:"+Collections.min(Arrays.asList(numbers)));
		System.out.println("最大值:"+Collections.max(Arrays.asList(numbers)));
		String a[] = { "A", "E", "I" };
        String b[] = { "O", "U" };
        List list=new ArrayList(Arrays.asList(a));
        list.addAll(Arrays.asList(b));
        System.out.println("数组合并:"+Arrays.toString(list.toArray()));
        
        String[] names=new String[]{"A","B","C"};
        String[] extended=new String[5];
        extended[3]="D";
        extended[4]="E";
        System.arraycopy(names, 0, extended, 0, names.length);
        System.out.println("数组扩容:"+Arrays.toString(extended));
        
        ArrayList objArray1=new ArrayList();
        objArray1.add(0,"common1");
        objArray1.add(1,"common2");
        objArray1.add(2,"notcommon2");
        ArrayList objArray2=new ArrayList();
        objArray2.add(0,"common1");
        objArray2.add(1,"common2");
        objArray2.add(2,"notcommon");
        objArray2.add(3,"notcommon1");
        objArray1.removeAll(objArray2);
        System.out.println("array1 与 array2 数组差集为："+objArray1);
        objArray1.retainAll(objArray2);
        System.out.println("array2 & array1 数组交集为："+objArray1);
        
        System.out.println("数组是否相等："+Arrays.equals(names, extended));
        
        

	}

}
